<?php
/**
 * Created by PhpStorm.
 * User: adminchyistrator
 * Date: 2018/12/8
 * Time: 14:13
 */

namespace app\adminchy\controller;

use think\facade\Request;
use app\adminchy\controller\Base;

class User extends Base
{
    private $field = 'user_id,user_name,user_phone,user_parent,user_truename,user_reg_code,number_id,user_gold,user_apliy_name,user_status,user_apliy_number,update_time';
    //用户列表
    public function index(){
        $search = Request::param('search');
        $sql = db('user')->field($this->field)->where('is_delete',0);
        if ($search){
            $sql->where('user_phone',$search)->whereOr('user_id',$search);
        }
        $userInfo=$sql->paginate(15)->each(function ($item,$key){
            $item['user_status'] =$item['user_status'] == 0 ? '未登陆过' :'已登陆过';
            $item['getSum'] 	= db('red_packet')->where(['user_id' => $item['user_id'], 'type' => 1])->sum('money');
            return $item;
        });
        $this->assign('search',$search);
        $this->assign('userInfo',$userInfo);
        return view('User/userList');
    }
    //用户信息
    public function userInfo(){
        $id = Request::param('user_id');
        $user = db('user')->field($this->field.',user_password,user_pmtpwd')->where('user_id',$id)->find();
        $this->assign('user', $user);
        return view('User/userInfo');
    }
    //用户编辑
    public function editUser(){

        $data = Request::post();
        // dump($data);die;
        if (!preg_match("/^1[34578]{1}\d{9}$/",$data['user_phone'])) {
            return json(['status' => 1, 'message' => '请填写正确的手机号']);
        }
            //手机号判断
            $phone = db('user')->where(['user_id'=>$data['user_id']])->value('user_phone');
            if ($phone == $data['user_phone']){
                unset($data['user_phone']);
            }else{
                $res = db('user')->where(['user_phone'=>$data['user_phone']])->find();
                if ($res){
                    return json(['status'=>1,'message'=>'该手机号已存在']);
                }
            }
            //密码判断
            if (empty($data['user_password'])) {
                unset($data['user_password']);
            }else{
                $data['user_password'] = md5($data['user_password']);
            }
            if (empty($data['user_pmtpwd'])) {
                unset($data['user_pmtpwd']);
            }else{
                $data['user_pmtpwd'] = md5($data['user_pmtpwd']);
            }
            //账户判断
            if(empty($data['user_gold']) || $data['user_gold'] < 0){
                unset($data['user_gold']);
            }
            $result =db('user')->where('user_id',$data['user_id'])->update($data);
            if ($result) {
                return json(['status' => 0, 'message' =>'修改成功!']);
            } else {
                return json(['status' => 1, 'message' =>'修改失败!']);
            }
        }
        //删除用户
        public function delete_user(){
            $user_id = Request::param('user_id');
            $res = db('user')->where(['user_id'=>$user_id])->find();
            if (!$res){
                return json(['status'=>1,'message'=>'该用户不存在']);
            }
            $delUser = db('user')->where(['user_id'=>$user_id])->update(['is_delete'=>1]);
            if ($delUser){
                return json(['status'=>0,'message'=>'删除用户成功']);
            }else{
                return json(['status'=>1,'message'=>'删除用户失败']);
            }
        }
        //推广信息  编号、手机号、昵称、推荐码、推广人数
        public function extendInfo(){
            $search =Request::param('search');
            $sql = db('user')->field('user_id,user_phone,user_name,user_reg_code,user_parent');
            if ($search){
                $sql->where('user_phone',$search)->whereOr('user_id',$search);
            }
            $extendInfo = $sql->paginate(15)->each(function ($item,$key){
                $item['extendsN'] = db('user')->where('user_parent',$item['user_id'])->count();
                return $item;
            });
            $this->assign('search',$search);
            $this->assign('extendInfo',$extendInfo);
            return view('User/extendInfo');
        }
}